package cn.iocoder.yudao.module.system.service.fieldsetting;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.system.controller.admin.fieldsetting.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.fieldsetting.FieldSettingDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.system.dal.mysql.fieldsetting.FieldSettingMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;

/**
 * 列表显示设置 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class FieldSettingServiceImpl implements FieldSettingService {

    @Resource
    private FieldSettingMapper fieldSettingMapper;

    @Override
    public Integer createFieldSetting(FieldSettingSaveReqVO createReqVO) {
        Long adminId = getLoginUserId();
        QueryWrapper<FieldSettingDO> wrapper = new QueryWrapper<>();
        wrapper.eq("creator",adminId);
        wrapper.eq("type",createReqVO.getType());
        FieldSettingDO add = fieldSettingMapper.selectOne(wrapper);
        if(add==null){
            FieldSettingDO fieldSetting = BeanUtils.toBean(createReqVO, FieldSettingDO.class);
            fieldSettingMapper.insert(fieldSetting);
        }else{
            createReqVO.setId(add.getId());
            FieldSettingDO updateObj = BeanUtils.toBean(createReqVO, FieldSettingDO.class);
            fieldSettingMapper.updateById(updateObj);
        }
        // 返回
        return 1;
    }

    @Override
    public void updateFieldSetting(FieldSettingSaveReqVO updateReqVO) {
        // 校验存在
        validateFieldSettingExists(updateReqVO.getId());
        // 更新
        FieldSettingDO updateObj = BeanUtils.toBean(updateReqVO, FieldSettingDO.class);
        fieldSettingMapper.updateById(updateObj);
    }

    @Override
    public void deleteFieldSetting(Integer id) {
        // 校验存在
        validateFieldSettingExists(id);
        // 删除
        fieldSettingMapper.deleteById(id);
    }

    @Override
        public void deleteFieldSettingListByIds(List<Integer> ids) {
        // 校验存在
        validateFieldSettingExists(ids);
        // 删除
        fieldSettingMapper.deleteByIds(ids);
        }

    private void validateFieldSettingExists(List<Integer> ids) {
        List<FieldSettingDO> list = fieldSettingMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(FIELD_SETTING_NOT_EXISTS);
        }
    }

    private void validateFieldSettingExists(Integer id) {
        if (fieldSettingMapper.selectById(id) == null) {
            throw exception(FIELD_SETTING_NOT_EXISTS);
        }
    }

    @Override
    public FieldSettingDO getFieldSetting(Integer type) {
        Long adminId = getLoginUserId();
        QueryWrapper<FieldSettingDO> wrapper = new QueryWrapper<>();
        wrapper.eq("creator",adminId);
        wrapper.eq("type",type);
        FieldSettingDO add = fieldSettingMapper.selectOne(wrapper);
        return add;
    }

    @Override
    public PageResult<FieldSettingDO> getFieldSettingPage(FieldSettingPageReqVO pageReqVO) {
        return fieldSettingMapper.selectPage(pageReqVO);
    }

}
